(function (window, $) {
    var OutlineApp = {};
    var returnData = new Array();
    var rootNodeData = new Array();
    var defalutConfig = {
        multi: false,//单选多选
        rootPath: basePath + "/outline/getRootOutlines.json",
        currentPath: basePath + "/outline/getOutLineListExt.json",
        questionListPath: basePath + "/outline/getQuestionList.json",
        questionDetailPath: basePath + "/question/getQuestionById.json",
        relatedQuestionPath: basePath + "/question/getRelatedQuestionById.json",
        selectType: "checkbox",
        currentTreeId: ""
    };
    var config = {};
    OutlineApp.Tree = {
        selectOutLine: function (exdconfig) {
            config = {};
            $.extend(true, config, defalutConfig, exdconfig);
            initRootNodes(config.currentTreeId, config.currentTreeId + "Selected", config.multi);
        },
        removeLi: function (id, treeId) {
            if (null != returnData && returnData.length > 0) {
                var i = 0;
                for (i = 0; i < returnData.length; i++) {
                    if (returnData[i].id == id) {
                        $("#" + treeId + id).remove();
                        var treeObj = $.fn.zTree.getZTreeObj(treeId);
                        var node = treeObj.getNodeByParam("id", id, null);
                        treeObj.checkNode(node, false, false);
                        returnData.splice(i, 1);
                        return true;
                    }
                }
            }
        },

    };

    var initRootNodes = function (_treeId, _selectTreeId, multi) {
        $.ajax({
            url: config.rootPath,
            type: "get",
            dataType: "json",
            success: function (r) {
//				var treeRNodes = r;
//				initTree(_treeId,_selectTreeId,treeRNodes);


                var treeRNodes = new Array();
                ;
                for (var key = 0; key < r.length; key++) {
                    var node = r[key];
                    var rNode = {};
                    rNode.content = node.rootName;
                    rNode.yearNum = node.rootYear;
                    rNode.outlineType = node.rootType;
                    rNode.isParent = true;
                    treeRNodes.push(rNode);
                }
                initTree(_treeId, _selectTreeId, treeRNodes);
                initSelectedNodes(_treeId);
            }
        })
            .fail(function () {
                BootstrapDialog.alert({title: "提示", message: "获取大纲数据异常，请联系管理员"});
            })

    };

    var defaultSetting = {
        view: {
            showLine: true,
            dblClickExpand: false,
            showIcon: true
        },
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "parentId",
                rootPId: null
            },
            key: {
                name: "content"
            }
        }
    };

    var initTree = function (_treeId, _selectTreeId, _treeZNodes) {
        var multi = config.multi;
        var settings = {};
        var userSetting = {
            check: {
                enable: true,
                autoCheckTrigger: true,
                chkboxType: {"Y": "s", "N": "ps"},
                chkStyle: config.selectType
            },
            async: {
                enable: true,
                url: config.currentPath,
                autoParam: ["id=parentId", "yearNum", "outlineType"],
                type: "get",
                dataFilter: function (treeId, parentNode, responseData) {
                    return responseData;
                },
            },
            callback: {
                onClick: function (event, treeId, treeNode) {
                    var zTree = $.fn.zTree.getZTreeObj(_treeId);
                    if (treeNode.isParent) {
                        zTree.expandNode(treeNode, "refresh");
                        return;
                    }
                    //如果是限选一人的情况
                    if (!multi) {
                        for (var i = 0; i < returnData.length; i++) {
                            var tId = returnData[i].tId.substring(0, returnData[i].tId.indexOf("_"));
                            if (_treeId == tId) {
                                returnData.splice(i, 1);
                            }
                        }
                        $("#" + _selectTreeId).empty();
                    }
                    //判断这个人有没有选择过
                    if (!treeNode.checked) {
                        addLi(treeNode, treeId, _selectTreeId);
                    } else {//如果是取消，则进行删除
                        OutlineApp.Tree.removeLi(treeNode.id, treeId);
                        if (!multi) {
                            var treeObj = $.fn.zTree.getZTreeObj(treeId);
                            var node = treeObj.getNodeByParam("id", treeNode.id, null);
                            treeObj.checkNode(node, false, false);
                        }
                    }
                },
                onCheck: zTreeOnCheck,
                beforeCheck: zTreeBeforeCheck,
            }
        };

        $.extend(true, settings, defaultSetting, userSetting);
        $.fn.zTree.destroy(_treeId);
        $.fn.zTree.init($("#" + _treeId), settings, _treeZNodes);
    };

    function initSelectedNodes(treeId) {
        var treeObj = $.fn.zTree.getZTreeObj(treeId);
        var nodes = treeObj.transformToArray(treeObj.getNodes());
    }

    function zTreeOnCheck(event, treeId, treeNode) {
        var multi = config.multi;
        var selectTreeId = treeId + "Selected";
        if (!treeNode.isParent) {
            if (!multi) {
                for (var i = 0; i < returnData.length; i++) {
                    var tId = returnData[i].tId.substring(0, returnData[i].tId.indexOf("_"));
                    if (treeId == tId) {
                        returnData.splice(i, 1);
                    }
                }
                $("#" + selectTreeId).empty();
            }
            //如果是选中，进行添加
            if (treeNode.checked) {
                addLi(treeNode, treeId, selectTreeId);
            } else {//如果是取消，则进行删除
                OutlineApp.Tree.removeLi(treeNode.id, treeId);
            }
        }
    };

    function zTreeBeforeCheck(treeId, treeNode) {
        var zTree = $.fn.zTree.getZTreeObj(treeId);
        if (treeNode.isParent) {
            zTree.expandNode(treeNode, true);
        }

    };

    function addLi(treeNode, treeId, selectTreeId) {
        var selectType = config.selectType;
        var isSelected = false;
        $("#" + selectTreeId).find("li").each(function (index, domEle) {
            var eId = $(domEle).attr("id");
            if (eId == treeId + treeNode.id) {
                isSelected = true;
            }
        });
        if (!isSelected) {
            var outlineId = treeNode.id;
            var str = "<li title='" + treeNode.content + "' id = '" + treeId + treeNode.id + "'><a href='javascript:void(0);' outlineId = '" + outlineId + "'  onclick = 'javascript:OutlineApp.Tree.removeLi(\"" + treeNode.id + "\",\"" + treeId + "\");'>" + treeNode.outlineCode + "----" + treeNode.content + "</a></li>";
            $("#" + selectTreeId).append(str);
            returnData.push(treeNode);
            var zTree = $.fn.zTree.getZTreeObj(treeId);
            var nodes = zTree.getCheckedNodes(true);
            if (selectType == "radio") {
                for (var i = 0, l = nodes.length; i < l; i++) {
                    zTree.checkNode(nodes[i], false, true);
                }
            }
            zTree.checkNode(treeNode, true, true);

        }

    };

    window.OutlineApp = OutlineApp;

})(window, jQuery);